#if false
#region License, Terms and Conditions
//
// Jayrock - A JSON-RPC implementation for the Microsoft .NET Framework
// Written by Atif Aziz (www.raboof.com)
// Copyright (c) Atif Aziz. All rights reserved.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
//
#endregion

namespace Jayrock.Json.Conversion.Generation
{
#if !NET_1_0 && !NET_1_1 && !NET_2_0

#region Imports

    using System;
    using NUnit.Framework;

    #endregion

    [TestFixture]
    public class TestImportResult
    {
        [Test]
        public void Serializable()
        {
            Assert.IsTrue(typeof(ImportResult<object>).IsSerializable);
        }

        [Test]
        public void DefaultInitialization()
        {
            Assert.IsFalse(new ImportResult<object>().Success);
        }

        [Test]
        public void Success()
        {
            var result = ImportResult.Success(123);
            Assert.IsTrue(result.Success);
            Assert.AreEqual(123, result.Value);
        }

        [Test, ExpectedException(typeof(InvalidOperationException))]
        public void CannotUseErrorWhenSuccess()
        {
            var result = ImportResult.Success(123);
            Assert.IsNull(result.Error);
        }

        [Test]
        public void Error()
        {
            var e = new Exception();
            var result = ImportResult.Error<object>(e);
            Assert.IsFalse(result.Success);
            Assert.AreEqual(e, result.Error);
        }

        [Test, ExpectedException(typeof(InvalidOperationException))]
        public void CannotUseValueWhenError()
        {
            var result = ImportResult.Error<object>(new Exception());
            Assert.IsNull(result.Value);
        }

        [Test]
        public void DefaultError()
        {
            var result = ImportResult.Error<object>();
            Assert.IsFalse(result.Success);
            Assert.IsAssignableFrom(typeof(JsonException), result.Error);
        }

        [Test]
        public void Return()
        {
            var importer = ImportResult.Success(42).Return();
            var result = importer(JsonText.CreateReader(string.Empty));
            Assert.IsTrue(result.Success);
            Assert.AreEqual(42, result.Value);
        }

        [Test, ExpectedException(typeof(ArgumentNullException))]
        public void CannotBindWithNullBinder()
        {
            ImportResult.Success(42).Bind<int>(null);
        }

        [Test]
        public void Bind()
        {
            var result = ImportResult.Success(42).Bind(n => ImportResult.Success(n * 2));
            Assert.IsTrue(result.Success);
            Assert.AreEqual(42 * 2, result.Value);
        }

        [Test]
        public void BindError()
        {
            var error = ImportResult.Error<int>();
            var result = error.Bind(_ => ImportResult.Success(42));
            Assert.IsFalse(result.Success);
            Assert.AreEqual(error.Error, result.Error);
        }
    }

#endif // !NET_1_0 && !NET_1_1 && !NET_2_0
}
#endif
#region License, Terms and Conditions
//
// Jayrock - A JSON-RPC implementation for the Microsoft .NET Framework
// Written by Atif Aziz (www.raboof.com)
// Copyright (c) Atif Aziz. All rights reserved.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
//
#endregion

namespace Jayrock.Json.Conversion.Generation
{
    #if !NET_1_0 && !NET_1_1 && !NET_2_0
    
    #region Imports

    using System;
    using NUnit.Framework;

    #endregion

    [TestFixture]
    public class TestImportState
    {
        [Test]
        public void Serializable()
        {
            Assert.IsTrue(typeof(ImportState).IsSerializable);
        }

        static JsonReader EmptyJsonReader()
        {
            return JsonText.CreateReader(string.Empty);
        }

        [Test]
        public void DefaultInitialization()
        {
            var state = ImportState.Create(EmptyJsonReader());
            Assert.IsFalse(state.IsInitialized);
        }

        [Test, ExpectedException(typeof(InvalidOperationException))]
        public void CannotUseErrorWhenUninitialized()
        {
            var state = ImportState.Create(EmptyJsonReader());
            Assert.IsFalse(state.IsInitialized);
            Assert.IsNull(state.Error);
        }

        [Test, ExpectedException(typeof(InvalidOperationException))]
        public void CannotUseIsSuccessWhenUninitialized()
        {
            var state = ImportState.Create(EmptyJsonReader());
            Assert.IsFalse(state.IsInitialized);
            Assert.IsFalse(state.Success);
        }

        [Test]
        public void Success()
        {
            var state = ImportState.CreateSuccess(EmptyJsonReader());
            Assert.IsTrue(state.IsInitialized);
            Assert.IsTrue(state.Success);
        }

        [Test, ExpectedException(typeof(InvalidOperationException))]
        public void CannotUseErrorWhenSuccess()
        {
            var state = ImportState.CreateSuccess(EmptyJsonReader());
            Assert.IsNull(state.Error);
        }

        [Test]
        public void Error()
        {
            var e = new Exception();
            var state = ImportState.CreateError(EmptyJsonReader(), e);
            Assert.IsTrue(state.IsInitialized);
            Assert.IsFalse(state.Success);
            Assert.AreEqual(e, state.Error);
        }

        [Test]
        public void DefaultError()
        {
            var state = ImportState.CreateError(EmptyJsonReader());
            Assert.IsTrue(state.IsInitialized);
            Assert.IsFalse(state.Success);
            Assert.IsAssignableFrom(typeof(JsonException), state.Error);
        }
        /*
        [Test]
        public void Return()
        {
            var importer = ImportResult.CreateSuccess().Return();
            var result = importer(JsonText.CreateReader(string.Empty));
            Assert.IsTrue(result.Success);
            Assert.AreEqual(42, result.Value);
        }
        */
        [Test, ExpectedException(typeof(ArgumentNullException))]
        public void CannotBindWithNullBinder()
        {
            ImportState.CreateSuccess(EmptyJsonReader()).Bind<int, int>(42, null);
        }

        [Test]
        public void Bind()
        {
            var state = ImportState.CreateSuccess(EmptyJsonReader());
            var value = state.Bind(42, n => n * 2);
            Assert.IsTrue(state.Success);
            Assert.AreEqual(42 * 2, value);
        }

        [Test]
        public void BindError()
        {
            var state = ImportState.CreateError(EmptyJsonReader());
            var value = state.Bind(-1, _ => 42);
            Assert.IsFalse(state.Success);
            Assert.AreEqual(0, value);
        }

        [Test]
        public void SetSuccess()
        {
            var state = ImportState.Create(EmptyJsonReader());
            Assert.IsFalse(state.IsInitialized);
            Assert.AreEqual(42, state.Succeeding(42));
            Assert.IsTrue(state.Success);
        }

        [Test]
        public void SetError()
        {
            var state = ImportState.Create(EmptyJsonReader());
            Assert.IsFalse(state.IsInitialized);
            Assert.IsNull(state.Failing<object>());
            Assert.IsFalse(state.Success);
            Assert.IsNotNull(state.Error);
            Assert.IsAssignableFrom(typeof(JsonException), state.Error);
        }

        [Test]
        public void SetErrorCustom()
        {
            var state = ImportState.Create(EmptyJsonReader());
            Assert.IsFalse(state.IsInitialized);
            var e = new Exception();
            Assert.IsNull(state.Failing<object>(e));
            Assert.IsFalse(state.Success);
            Assert.AreEqual(e, state.Error);
        }
    }

    #endif // !NET_1_0 && !NET_1_1 && !NET_2_0
}